"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.hideInstance = exports.WebLayout = void 0;

var _react = _interopRequireWildcard(require("react"));

var _viewerjs = _interopRequireDefault(require("viewerjs"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var ViewerInstance = /*#__PURE__*/function () {
  function ViewerInstance() {
    var _this = this;

    _classCallCheck(this, ViewerInstance);

    _defineProperty(this, "instance", {});

    _defineProperty(this, "rootRef", null);

    _defineProperty(this, "activeInstance", null);

    _defineProperty(this, "onCloseImpl", function () {});

    // not ssr
    if (typeof document !== 'undefined' && typeof window !== 'undefined') {
      var viewerDom = document.createElement('div');
      viewerDom.classList.add('viewer-root-element');
      this.rootRef = viewerDom;
      this.instance = new _viewerjs.default(viewerDom, {
        hide: function hide() {
          _this.onCloseImpl();
        }
      });
      var bodyDom = document.getElementsByTagName('body')[0];
      bodyDom.appendChild(viewerDom);
    }
  }

  _createClass(ViewerInstance, [{
    key: "hideCommand",
    value: function hideCommand() {
      if (this.instance) {
        this.instance.hide();
      }
    }
  }, {
    key: "hide",
    value: function hide(target) {
      if (target !== this.activeInstance) return;
      this.hideCommand();
    }
  }, {
    key: "show",
    value: function show(index) {
      if (this.instance.view) {
        this.instance.view(index);
      }
    }
  }, {
    key: "destroy",
    value: function destroy() {
      if (this.instance.destroy) {
        this.instance.destroy();
      }
    }
  }, {
    key: "createImgElement",
    value: function createImgElement(src) {
      if (typeof document !== 'undefined' && typeof window !== 'undefined') {
        var dupImg = new Image();
        dupImg.src = src;
        dupImg.setAttribute('style', 'display: none');
        return dupImg;
      }
    }
  }, {
    key: "setImgs",
    value: function setImgs(imgsWrapperDomRef) {
      if (typeof document !== 'undefined' && typeof window !== 'undefined') {
        // 如果当前元素和目前active元素一致，复用 dom
        var imgsDom = imgsWrapperDomRef.getElementsByTagName('img');
        var frag = document.createDocumentFragment();
        this.rootRef.innerHTML = '';
        Array.from(imgsDom).forEach(function (element) {
          var clonedElement = element.cloneNode();
          clonedElement.setAttribute('style', 'display: none');
          frag.appendChild(clonedElement);
        });
        this.rootRef.appendChild(frag);
        this.instance.update();
      }
    }
  }, {
    key: "registerOnClose",
    value: function registerOnClose() {
      var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
      this.onCloseImpl = callback;
    }
  }]);

  return ViewerInstance;
}();

var viewerInstanceSingleton = new ViewerInstance();
var WebLayout = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var _ref$imgs = _ref.imgs,
      imgs = _ref$imgs === void 0 ? [] : _ref$imgs,
      _ref$currentUrl = _ref.currentUrl,
      currentUrl = _ref$currentUrl === void 0 ? '' : _ref$currentUrl,
      visible = _ref.visible,
      _ref$onClose = _ref.onClose,
      onClose = _ref$onClose === void 0 ? function () {} : _ref$onClose;

  var viewerDomRef = _react.default.useRef(null);

  var currentImgIndex = _react.default.useRef(0);

  var show = (0, _react.useCallback)(function () {
    if (viewerInstanceSingleton && viewerInstanceSingleton.show) {
      viewerInstanceSingleton.registerOnClose(onClose);

      if (viewerDomRef.current) {
        viewerInstanceSingleton.setImgs(viewerDomRef.current);
        viewerInstanceSingleton.show(currentImgIndex.current);
      }

      viewerInstanceSingleton.activeInstance = viewerDomRef.current;
    }
  }, [imgs, onClose]);
  var hide = (0, _react.useCallback)(function () {
    if (viewerInstanceSingleton && viewerInstanceSingleton.hide) {
      viewerInstanceSingleton.hide(viewerDomRef.current);
    }
  }, []);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      show: show,
      hide: hide
    };
  }, [show, hide]);
  (0, _react.useEffect)(function () {
    if (viewerInstanceSingleton) {
      if (visible) {
        show();
      } else {
        viewerInstanceSingleton.hide(viewerDomRef.current);
      }
    }
  }, [visible]);
  (0, _react.useEffect)(function () {
    var currentIndex = imgs.indexOf(currentUrl);
    if (currentIndex === -1) currentIndex = 0;
    currentImgIndex.current = currentIndex;
  }, [currentUrl, imgs]);
  return /*#__PURE__*/_react.default.createElement("div", {
    ref: viewerDomRef,
    style: {
      display: 'none'
    }
  }, imgs.map(function (imgUrl, idx) {
    return /*#__PURE__*/_react.default.createElement("img", {
      alt: "\u56FE\u7247",
      key: idx,
      src: imgUrl
    });
  }));
}); // 隐藏 viewer 实例

exports.WebLayout = WebLayout;

var hideInstance = function hideInstance() {
  viewerInstanceSingleton.hideCommand();
};

exports.hideInstance = hideInstance;